#include "dispatcher.hpp"

void Dispatcher::AddConsumer(int fd, int event, EventHandler&& handler)
{
    if (handlerMap.count(fd) > 0)
    {
        reactor.ModFd(fd, event);
    }
    else
    {
        reactor.AddFd(fd, event);
    }
    handlerMap[fd] = std::move(handler);
}
void Dispatcher::RmvConsumer(int fd)
{
    if (handlerMap.count(fd) > 0)
    {
        reactor.RmvFd(fd);
        handlerMap.erase(fd);
    }
}
void Dispatcher::Poll()
{
    std::vector<int> activeFds;
    while (active)
    {
        activeFds.clear();
        reactor.Poll(activeFds);
        for (int fd : activeFds)
        {
            handlerMap[fd](Socket(fd));         // you can execute this callback in another thread (such as business thread)
        }
    }
}
